package com.wxg;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.wxg.druid.util.DruidUtils;

public class DruidDemo {
	
	private static final String DRUID_CONFIG = "druid/config01.properties";

	public static void main(String[] args) throws Exception {
		DruidDemo dd = new DruidDemo();
//		dd.test01();
//		dd.sampleUseDruid();
//		dd.test03();
//		dd.sampleDruidDemo();
		dd.multipleDruidDemo();
	}
	
	public void multipleDruidDemo(){
		DataSource ds = DruidUtils.fetchDS(DRUID_CONFIG);
		
		String sql = null;
		try {
			Map<String,String> sqlMap = new LinkedHashMap<String,String>();
			sqlMap.put("api_app", "SELECT COUNT(1) FROM api_app");
			sqlMap.put("api_interface", "SELECT COUNT(1) FROM api_interface");
			sqlMap.put("api_sub_interface", "SELECT COUNT(1) FROM api_sub_interface");
			sqlMap.put("api_user", "SELECT COUNT(1) FROM api_user");
			
			for(int i=0;i<5;i++){
				for(String key : sqlMap.keySet()){
					
					Connection conn = ds.getConnection();
					System.out.println(String.format("=========conn: %s", conn.hashCode()));
					Statement stmt = conn.createStatement();
					sql = sqlMap.get(key);
					ResultSet rs = stmt.executeQuery(sql);
					while(rs.next()){
						System.out.println(String.format("%s count: %s", key, rs.getString(1)));
					}
					conn.close();
					
				}
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			ds = null;
		}
	}
	
	/**
	 * 此方法可以运行
	 */
	public void sampleDruidDemo(){
		Properties prop = DruidUtils.loadProp(DRUID_CONFIG);
		try {
			DataSource ds = DruidDataSourceFactory.createDataSource(prop);
			Connection conn = ds.getConnection();
			Statement stmt = conn.createStatement();
			String sql = "SELECT COUNT(1) FROM api_app";
			ResultSet rs = stmt.executeQuery(sql);
			System.out.println("======");
			while(rs.next()){
				System.out.println(rs.getInt(1));
			}
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 只是测试properties的输出
	 * @throws Exception
	 */
	public void test01() throws Exception{
		String config = DRUID_CONFIG;
		Properties prop = DruidUtils.loadProp(config);
		prop.list(System.out);
	}
	
	
	/**
	 * 此方法不能正常运行
	 */
	public void sampleUseDruidErrorExample(){
		DruidDataSource dds = new DruidDataSource();
		dds.getConnectProperties().list(System.out);
		
		System.out.println(String.format("dds.isInited() : %s", dds.isInited()));
		
		dds.addConnectionProperty("username", "root");
		dds.addConnectionProperty("password", "root");
		dds.addConnectionProperty("jdbcUrl", "jdbc:mysql://127.0.0.1:3306/openapi");
//		driverClass
//		driverClass
//		dds.addConnectionProperty("driverClass", "com.mysql.jdbc.Driver");
		
		System.out.println("------");
		dds.getConnectProperties().list(System.out);
		System.out.println("------");
		System.out.println( dds.getUrl() );
		System.out.println( dds.getUsername() );
		
		runSqlQuery(dds);
		
	}
	
	/**
	 * 此方法运行出错
	 */
	public void druidDataSourceErrorInvoke(){
		Properties prop = DruidUtils.loadProp(DRUID_CONFIG);
		DruidDataSource dds = new DruidDataSource();
		dds.configFromPropety(prop);
		runSqlQuery(dds);
		
	}
	
	/**
	 * private : 仅仅是少输入些代码
	 * @param dds
	 */
	private void runSqlQuery(DruidDataSource dds){
		try {
			DruidPooledConnection druidpool = dds.getConnection();
			Connection conn = druidpool.getConnection();
			Statement stmt = conn.createStatement();
			String sql = "SELECT COUNT(1) FROM api_app";
			ResultSet rs = stmt.executeQuery(sql);
			System.out.println("======");
			while(rs.next()){
				System.out.println(rs.getInt(1));
			}
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			dds.close();
		}
	}
	
	public void testMap(){
		Map<String,String> sqlMap = new LinkedHashMap<String,String>();
		sqlMap.put("api_app", "SELECT COUNT(1) FROM api_app");
		sqlMap.put("api_interface", "SELECT COUNT(1) FROM api_interface");
		sqlMap.put("api_sub_interface", "SELECT COUNT(1) FROM api_sub_interface");
		sqlMap.put("api_user", "SELECT COUNT(1) FROM api_user");
		for(String key : sqlMap.keySet()){
			System.out.println(String.format("key: %s, val: %s", key, sqlMap.get(key)));
		}
	}
}
